package com.haoshui.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.haoshui.domain.ResponseResult;
import com.haoshui.domain.entity.Link;
import com.haoshui.domain.vo.PageVo;
import com.haoshui.enums.AppHttpCodeEnum;
import com.haoshui.service.LinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
public class LinkController {

    @Autowired
    private LinkService linkService;

    /**
     * 页查询友链，如果有条件就模糊查询
     * @param pageNum
     * @param pageSize
     * @param name
     * @param status
     * @return
     */
    @GetMapping("/content/link/list")
    @PreAuthorize("@ps.hasPermission('content:link:list')")
    public ResponseResult pageCategory(Integer pageNum, Integer pageSize, String name, String status){
        //分页查询
        Page<Link> page =  linkService.pageLink(pageNum, pageSize, name, status);

        //封装Vo
        PageVo pageVo = new PageVo(page.getRecords(), page.getTotal());

        return ResponseResult.okResult(pageVo);

    }


    /**
     * 添加link
     * @param link
     * @return
     */
    @PostMapping("/content/link")
    @PreAuthorize("@ps.hasPermission('content:link:add')")
    public ResponseResult addCategory(@RequestBody Link link){
        //存储
        boolean save = linkService.save(link);

        if(save){
            return ResponseResult.okResult();
        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.INSERT_FAILED);
        }
    }


    /**
     * 修改link，查询步骤
     * @param id
     * @return
     */
    @GetMapping("/content/link/{id}")
    @PreAuthorize("@ps.hasPermission('content:link:edit')")
    public ResponseResult findCategory(@PathVariable("id") Long id){
        //查询分类
        Link link = linkService.getById(id);

        //返回
        return ResponseResult.okResult(link);
    }


    /**
     * 修改link，更新步骤
     * @param category
     * @return
     */
    @PutMapping("/content/link")
    @PreAuthorize("@ps.hasPermission('content:link:edit')")
    public ResponseResult updateCategory(@RequestBody Link link){
        //更新
        boolean b = linkService.updateById(link);

        if(b){
            return ResponseResult.okResult();
        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.UPDATE_FAILED);
        }

    }


    /**
     * 删除link，逻辑删除
     * @param id
     * @return
     */
    @DeleteMapping("/content/link/{id}")
    @PreAuthorize("@ps.hasPermission('content:link:remove')")
    public ResponseResult deleteCategory(@PathVariable("id") String id){
        //删除
        boolean b = linkService.removeById(id);

        if(b){
            return ResponseResult.okResult();
        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.DELETE_FAILED);
        }
    }

    /**
     * 更新状态
     * @param link
     * @return
     */
    @PutMapping("/content/link/changeLinkStatus")
    @PreAuthorize("@ps.hasPermission('content:link:list')")
    public ResponseResult changeStatus(@RequestBody Link link){
        boolean b = linkService.updateById(link);

        if(b){
            return ResponseResult.okResult();
        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.UPDATE_FAILED);
        }
    }
}
